{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------style :\n",
      "t: 0\n",
      "t: 1\n",
      "t: 2\n",
      "t: 3\n",
      "self.browser form submit faild...\n",
      "None\n",
      "my name is cookie : None\n",
      "write cookies_str faild...\n",
      "CDwindow-59c07eb8-93ad-44ee-b61b-ccc27629a5dc\n"
     ]
    },
    {
     "ename": "WebDriverException",
     "evalue": "Message: chrome not reachable\n  (Session info: chrome=58.0.3029.81)\n  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.10586 x86_64)\n",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mWebDriverException\u001b[0m                        Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-22f8fac2f0d9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m    218\u001b[0m \u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    219\u001b[0m \u001b[0mjs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'window.open(\"http://www.taobao.com\")'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 220\u001b[1;33m \u001b[0mlogin\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbrowser\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute_script\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mjs\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 执行js打开另一个窗口\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    221\u001b[0m \u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    222\u001b[0m \u001b[0mhandles\u001b[0m \u001b[1;33m=\u001b[0m  \u001b[0mlogin\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbrowser\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwindow_handles\u001b[0m \u001b[1;31m# 所有窗口句柄集合(编号集合)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\python27\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.pyc\u001b[0m in \u001b[0;36mexecute_script\u001b[1;34m(self, script, *args)\u001b[0m\n\u001b[0;32m    482\u001b[0m         return self.execute(command, {\n\u001b[0;32m    483\u001b[0m             \u001b[1;34m'script'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mscript\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 484\u001b[1;33m             'args': converted_args})['value']\n\u001b[0m\u001b[0;32m    485\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    486\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mexecute_async_script\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscript\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\python27\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.pyc\u001b[0m in \u001b[0;36mexecute\u001b[1;34m(self, driver_command, params)\u001b[0m\n\u001b[0;32m    247\u001b[0m         \u001b[0mresponse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcommand_executor\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdriver_command\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparams\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    248\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mresponse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 249\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0merror_handler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcheck_response\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    250\u001b[0m             response['value'] = self._unwrap_value(\n\u001b[0;32m    251\u001b[0m                 response.get('value', None))\n",
      "\u001b[1;32md:\\python27\\lib\\site-packages\\selenium\\webdriver\\remote\\errorhandler.pyc\u001b[0m in \u001b[0;36mcheck_response\u001b[1;34m(self, response)\u001b[0m\n\u001b[0;32m    191\u001b[0m         \u001b[1;32melif\u001b[0m \u001b[0mexception_class\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mUnexpectedAlertPresentException\u001b[0m \u001b[1;32mand\u001b[0m \u001b[1;34m'alert'\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    192\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mexception_class\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscreen\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstacktrace\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'alert'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'text'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 193\u001b[1;33m         \u001b[1;32mraise\u001b[0m \u001b[0mexception_class\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscreen\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstacktrace\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    194\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    195\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_value_or_default\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdefault\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mWebDriverException\u001b[0m: Message: chrome not reachable\n  (Session info: chrome=58.0.3029.81)\n  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.10586 x86_64)\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from time import sleep\n",
    "import time\n",
    "from selenium.webdriver.common.action_chains import ActionChains\n",
    "from selenium.webdriver.common.keys import Keys\n",
    "import requests\n",
    "\n",
    "class LoginAlimamaGetCookie(object):\n",
    "    '''\n",
    "    Class doc\n",
    "    登录阿里妈妈,获取cookie\n",
    "    '''\n",
    "    def __init__(self, user=None, passwd=None, url=None, path='C:\\cookie_str.txt'):\n",
    "        '''\n",
    "        Function doc\n",
    "        类的初始化,定义一些参数\n",
    "        '''\n",
    "        self.user = user\n",
    "        self.passwd = passwd\n",
    "        self.login_url = \"https://login.taobao.com/member/login.jhtml?style=mini&from=alimama\" if not url else url\n",
    "        self.path = path\n",
    "        self.browser = None\n",
    "        self.times = 3\n",
    "\n",
    "\n",
    "    def get_cookies_str(self):\n",
    "        '''\n",
    "        Function doc\n",
    "        启动真实浏览器,登录淘宝,获取cookie信息\n",
    "        '''\n",
    "        # 调用无端浏览器\n",
    "        # self.browser = webdriver.PhantomJS(executable_path='E:\\\\Workspace\\\\Python_packages\\\\phantomjs-2.1.1-windows\\\\bin\\\\phantomjs.exe')\n",
    "        # options = self.browser_options()\n",
    "        # 谷歌\n",
    "        self.browser = webdriver.Chrome()\n",
    "        # 火狐\n",
    "#         self.browser = webdriver.Firefox()\n",
    "        style = None\n",
    "        cookies_str = None\n",
    "        self.browser.get(self.login_url)\n",
    "        time.sleep(2)\n",
    "\n",
    "        try:\n",
    "            # 输入用户名\n",
    "            self.browser.find_element_by_id(\"TPL_username_1\").click()\n",
    "            time.sleep(1)\n",
    "            for i in self.user:\n",
    "                time.sleep(0.1)\n",
    "                self.browser.find_element_by_id(\"TPL_username_1\").send_keys(u\"%s\"%i)\n",
    "\n",
    "            # 模拟点击动作(用户名框)\n",
    "            self.browser.find_element_by_id(\"TPL_username_1\").click()\n",
    "            time.sleep(1)\n",
    "            # 模拟点击动作(密码框)\n",
    "            self.browser.find_element_by_id(\"TPL_password_1\").click()\n",
    "            time.sleep(1)\n",
    "            # 查看滑块属性是否隐藏\n",
    "            try:\n",
    "                style = self.browser.find_element_by_id(\"nocaptcha\").get_attribute('style')\n",
    "            except:\n",
    "                pass\n",
    "            print '--------style :%s'%style\n",
    "            # 滑块验证\n",
    "            if style == 'display: block;':\n",
    "                # with open('page.html','w') as f:\n",
    "                # \tf.write(self.browser.page_source.encode('utf8'))\n",
    "                try:\n",
    "                    # 滑块验证\n",
    "                    time.sleep(1)\n",
    "                    self.need_nocaptcha()\n",
    "                    time.sleep(2)\n",
    "                    # 验证失败\n",
    "                    if self.browser.page_source.find('class=\"errloading\"') !=-1:\n",
    "                        print '-----nocaptcha errloading!'\n",
    "                        self.browser.close()\n",
    "                        time.sleep(1)\n",
    "                        if self.times > 0:\n",
    "                            self.times -= 1\n",
    "                            cookies_str = self.get_cookies_str()\n",
    "                            return cookies_str\n",
    "                    print 'nocaptcha success...'\n",
    "                except:\n",
    "                    pass\n",
    "\n",
    "            # 模拟点击动作(密码框)\n",
    "            self.browser.find_element_by_id(\"TPL_password_1\").click()\n",
    "            # 输入密码\n",
    "            for i in self.passwd:\n",
    "                time.sleep(0.1)\n",
    "                self.browser.find_element_by_id(\"TPL_password_1\").send_keys(i)\n",
    "            # 点击提交按钮\n",
    "            time.sleep(2)\n",
    "            try:\n",
    "                self.browser.find_element_by_id(\"J_SubmitStatic\").click()\n",
    "            except:\n",
    "                pass\n",
    "\n",
    "            # 最多等待页面加载10秒\n",
    "            t = 0\n",
    "            while t < 5:\n",
    "                print 't: %s'%t\n",
    "                time.sleep(1)\n",
    "                if self.browser.current_url.find('www.alimama.com') !=-1:\n",
    "                    # 登录成功返回cookies_str\n",
    "                    cookies = self.browser.get_cookies()\n",
    "                    print '-------get cookies success'\n",
    "                    time.sleep(0.1)\n",
    "                    cookies_str = ';'.join([\"%s@@@%s\" % (cookie['name'], cookie['value']) for cookie in cookies])\n",
    "                    print cookies_str\n",
    "                    break\n",
    "                t += 1\n",
    "        except:\n",
    "            print 'self.browser form submit faild...'\n",
    "        print cookies_str\n",
    "        return cookies_str\n",
    "\n",
    "\n",
    "    def write_cookie(self, cookies_str=None):\n",
    "        '''\n",
    "        Function doc\n",
    "        写cookies\n",
    "        '''\n",
    "        if cookies_str:\n",
    "            with open(self.path,'w') as f:\n",
    "                f.write(cookies_str)\n",
    "                return cookies_str\n",
    "        # 取到cookie\n",
    "        cookies_str = self.get_cookies_str()\n",
    "        print 'my name is cookie : %s'%cookies_str\n",
    "        if cookies_str:\n",
    "            print 'write cookies_str success...'\n",
    "            with open(self.path,'w') as f:\n",
    "                f.write(cookies_str)\n",
    "                return cookies_str\n",
    "        else:\n",
    "            print 'write cookies_str faild...'\n",
    "            with open(self.path,'w') as f:\n",
    "                f.write('')\n",
    "            return\n",
    "\n",
    "    def read_cookie(self):\n",
    "        '''\n",
    "        Function doc\n",
    "        读cookie\n",
    "        '''\n",
    "        try:\n",
    "            with open(self.path,'r') as f:\n",
    "                return f.read()\n",
    "        except:\n",
    "            print 'read [%s] faild...' % self.path\n",
    "            return\n",
    "\n",
    "    def need_nocaptcha(self):\n",
    "        '''\n",
    "        Function doc\n",
    "        滑块验证\n",
    "        '''\n",
    "        # 滑块\n",
    "        e = self.browser.find_element_by_id(\"nc_1_n1z\")\n",
    "        # 坐标\n",
    "        # x = e.location['x']\n",
    "        # y = e.location['y']\n",
    "        action = ActionChains(self.browser)\n",
    "        # 抓住滑块\n",
    "        action.click_and_hold(on_element=e)\n",
    "        time.sleep(1)\n",
    "        # 移动滑块\n",
    "        x = 5\n",
    "        y = 1\n",
    "        for i in range(70):\n",
    "            x += 5\n",
    "            if i <= 35:\n",
    "                y += 1\n",
    "                action.move_to_element_with_offset(to_element=e, xoffset=x, yoffset=y)\n",
    "            else:\n",
    "                y -= 1\n",
    "                action.move_to_element_with_offset(to_element=e, xoffset=x, yoffset=y)\n",
    "        try:\n",
    "            # 执行移动动作\n",
    "            action.perform()\n",
    "            # 释放滑块\n",
    "            action.release(e)\n",
    "        except:\n",
    "            pass\n",
    "\n",
    "    def browser_options(self):\n",
    "        '''\n",
    "        Function doc\n",
    "        浏览器配置\n",
    "        '''\n",
    "        # 添加参数实例\n",
    "        options = webdriver.ChromeOptions()\n",
    "        # 添加用户配置文件\n",
    "        # options.add_argument('--user-data-dir=C:\\Users\\you\\AppData\\Local\\Google\\Chrome\\User Data')\n",
    "        # 伪装手机端\n",
    "        # options.add_argument('--user-agent=iphone')\n",
    "        options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0')\n",
    "        # 加载插件\n",
    "        # options.add_extension('C:\\Users\\you\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Extensions')\n",
    "        # 调用谷歌浏览器\n",
    "        return options\n",
    "\n",
    "    def browser_refresh(self):\n",
    "        '''\n",
    "        Function doc\n",
    "        刷新页面\n",
    "        '''\n",
    "        # 刷新页面\n",
    "        # self.browser.find_element_by_id(\"TPL_username_1\").send_keys(Keys.F5)\n",
    "        # 清空用户名的表单内容\n",
    "        time.sleep(1)\n",
    "        self.browser.find_element_by_id(\"TPL_username_1\").clear()\n",
    "\n",
    "\n",
    "login = LoginAlimamaGetCookie(user='meilyxiao',passwd='dfst5088',path='D:\\\\cookie_str.txt')\n",
    "login.write_cookie()\n",
    "print login.browser.current_window_handle # 当前窗口句柄(编号)\n",
    "sleep(1)\n",
    "js = 'window.open(\"http://www.taobao.com\")'\n",
    "login.browser.execute_script(js) # 执行js打开另一个窗口\n",
    "sleep(3)\n",
    "handles =  login.browser.window_handles # 所有窗口句柄集合(编号集合)\n",
    "login.browser.switch_to_window(handles[1]) # 切换窗口\n",
    "\n",
    "import requests\n",
    "import json\n",
    "import re\n",
    "import random\n",
    "\n",
    "def coupon_urls():\n",
    "    url = \"http://admin.zhuandanku.com/api.php/listtransfer/Tngoodsinfo/onSaleGoodsCouponLink\"\n",
    "    r = requests.get(url)\n",
    "    data = json.loads(r.content)\n",
    "    data = data['data']\n",
    "    return data\n",
    "\n",
    "def get_sellerId_and_activityId(url):\n",
    "    m = re.search(r'activity_id=.*|activityId=.*',url)\n",
    "    activityId = m.group().split('=')[1][:32] if m else ''\n",
    "    m = re.search(r'seller_id=.*|sellerId=.*',url)\n",
    "    m = re.search(r'\\d+',m.group())\n",
    "    sellerId = m.group() if m else ''\n",
    "    return activityId,sellerId\n",
    "\n",
    "url = 'https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=4161a8c8137a4ca69f56373282a35438&seller_id=3165443959'\n",
    "# data = coupon_urls()\n",
    "# url_model = 'https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=%s&seller_id=%s'\n",
    "# for d in data:\n",
    "#     url = d['couponLink']\n",
    "#     # 获取卖家id,优惠券id\n",
    "#     url = url_model%(get_sellerId_and_activityId(url))\n",
    "#     sleep(0.8)\n",
    "#     login.browser.get(url)\n",
    "\n",
    "# while True:\n",
    "#     login.browser.get(url)\n",
    "#     sleep(random.choice(range(600,999)))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(144, 233, 0)\n"
     ]
    }
   ],
   "source": [
    "\n",
    "def add(a,b,n):\n",
    "    while n > 0:\n",
    "        a,b = b,a+b\n",
    "        n -= 1\n",
    "        add(a,b,n)\n",
    "    return a,b,n\n",
    "\n",
    "print add(1,2,10)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CDwindow-79476f3c-4dff-4a37-98fb-6370e584bac4\n",
      "[u'CDwindow-79476f3c-4dff-4a37-98fb-6370e584bac4', u'CDwindow-ff12a044-4329-4d94-80e8-b672bd862bc1']\n",
      "[u'CDwindow-79476f3c-4dff-4a37-98fb-6370e584bac4', u'CDwindow-ff12a044-4329-4d94-80e8-b672bd862bc1']\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "\n",
    "browser = webdriver.Chrome()\n",
    "browser.get(\"http://www.baidu.com\")\n",
    "js = 'window.open(\"http://www.taobao.com\")'\n",
    "print browser.current_window_handle #当前窗口句柄\n",
    "browser.execute_script(js)\n",
    "handles = browser.window_handles # 所有窗口句柄集合\n",
    "print handles\n",
    "browser.switch_to_window(handles[1]) # 切换到第二个窗口 关闭窗口\n",
    "browser.get(\"http://zhuandanku.com\")\n",
    "sleep(5)\n",
    "print browser.window_handles \n",
    "browser.quit()\n",
    "\n",
    "# # js = 'window.open(\"http://www.taobao.com\")'\n",
    "# browser.switch_to_window(handles[0]) # 切换回原来的页面\n",
    "# browser.execute_script(js)\n",
    "# sleep(3)\n",
    "# print handles\n",
    "# print browser.window_handles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=4161a8c8137a4ca69f56373282a35438&seller_id=3165443959\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=a1ffee1c5e4e495f92be70929fb1413b&seller_id=1969351117\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=50eb40a441f34beaa3efb5dd0ee38819&seller_id=2258006982\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=47912f57bfca4306830b63b26468ca9d&seller_id=1934824408\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=f52bf5c87b0a4a15a5e2891312811ef9&seller_id=1867628079\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=59e96ed3199b4ab39776cd2282af5bab&seller_id=1699225083\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=9b5b3141bda94e73808d9c8db4f67ef4&seller_id=3160299498\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=39201b6e47cb403b8de681160c1cb6cd&seller_id=2422350211\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=b028ab46ccf0494193eab426e9b6c822&seller_id=2472165025\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=286a420f4c80481cb983bf1f63e84deb&seller_id=884233631\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=e018df870a744a1eb440689b6450a3b9&seller_id=746173362\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=fa2e3dafb33c482c949f5b2b9fd5f65d&seller_id=2862563718\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=728c9488c5c04952904746a301bc705a&seller_id=3115363036\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=1e6e65db329f42dcb663924bb964bf80&seller_id=1757494582\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=57ae264815bd45f28d6af114db6cff46&seller_id=2452424348\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=90c77bc0b4fb44fa9b8b81cd7a9066a1&seller_id=54602397\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=cf2a4ad3172f4b69b9492eefdc6a937b&seller_id=3105103218\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=4b8cc70f103c4292b6612262fc7552e7&seller_id=365378308\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=c1697a9c75964164968f52d5ab7471e9&seller_id=2773692553\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=0213abc3e75e47b7850501f2532adf88&seller_id=772753017\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=24af1a55f8e24766adbf53f6226e463f&seller_id=296454738\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=758d460240114486abcea2b950a910f8&seller_id=1818927048\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=2d080d9d986a4795a83377e5bfc3edfe&seller_id=740551234\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=066da687266e4b7f9eb9d85eaf0e96cf&seller_id=745230131\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=a28f8847d8b74d729a177f534ca29147&seller_id=2928324129\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=35cff44cff87499c9761af51680416d4&seller_id=2112828639\n",
      "https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=9461d06b758f40b486b67ab7f4242a80&seller_id=2150206638\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "import re\n",
    "\n",
    "def coupon_urls():\n",
    "    url = \"http://admin.zhuandanku.com/api.php/listtransfer/Tngoodsinfo/onSaleGoodsCouponLink\"\n",
    "    r = requests.get(url)\n",
    "    data = json.loads(r.content)\n",
    "    data = data['data']\n",
    "    return data\n",
    "\n",
    "def get_sellerId_and_activityId(url):\n",
    "    m = re.search(r'activity_id=.*|activityId=.*',url)\n",
    "    activityId = m.group().split('=')[1][:32] if m else ''\n",
    "    m = re.search(r'seller_id=.*|sellerId=.*',url)\n",
    "    m = re.search(r'\\d+',m.group())\n",
    "    sellerId = m.group() if m else ''\n",
    "    return activityId,sellerId\n",
    "\n",
    "data = coupon_urls()\n",
    "url_model = 'https://market.m.taobao.com/apps/aliyx/coupon/detail.html?wh_weex=true&activity_id=%s&seller_id=%s'\n",
    "for d in data:\n",
    "    url = d['couponLink']\n",
    "    # 获取卖家id,优惠券id\n",
    "    url = url_model%(get_sellerId_and_activityId(url))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "750"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "random.choice(range(600,999))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import urllib2\n",
    "url = 'https://api.m.taobao.com/h5/mtop.taobao.couponmtopreadservice.findshopbonusactivitys/2.0/?appKey=12574478&t=1494380654882&sign=af424a69ae3dfbf56ba3cf229237f9d7&api=mtop.taobao.couponMtopReadService.findShopBonusActivitys&v=2.0&AntiFlood=true&ecode=1&type=jsonp&dataType=jsonp&callback=mtopjsonp2&data={\"uuid\":\"484d7b9f1ae9475985825ff46644d51e\",\"sellerId\":\"484d7b9f1ae9475985825ff46644d51e\",\"queryShop\":true}'\n",
    "\n",
    "print urllib2.quote(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
